<?php
/**
 *  APP相关设置
 */
namespace Clientside\Controller;

use Common\Controller\AdminbaseController;

class AdminSettingsController extends AdminbaseController {

    protected $sign_model;
    protected $interface_setting_model;
    protected $interface_model;
    protected $taxonomys=array("park"=>"停车系统","erp"=>"ERP系统");

    function _initialize() {
        parent::_initialize();
        $this->sign_model = D("Clientside/Sign");
        $this->interface_setting_model = D("Options");
        $this->interface_model = D("Interface");
        $this->assign("taxonomys",$this->taxonomys);
    }

    /**
     * 签到设置
     * @return [type] [description]
     */
    public function sign()
    {
        $post = $this->sign_model->find();
        $this->assign('post',$post);
        $this->display();
    }
    /**
     * 签到设置提交
     * @return [type] [description]
     */
    public function sign_post(){
        if (IS_POST) {
            $post_id=intval($_POST['post']['id']);

            $_POST['post']['update_time']=date("Y-m-d H:i:s",time());
            $sign=I("post.post");
            $sign['sign_description']=htmlspecialchars_decode($sign['sign_description']);
            $result=$this->sign_model->where(['id'=>$post_id])->save($sign);
            if ($result!==false) {
                $this->success("保存成功！",U("AdminSettings/sign"));
            } else {
                $this->error("保存失败！");
            }
        }
    }

    /**
     * 停车系统设置
     * @return [type] [description]
     */
    public function park_setting()
    {
        $options = $this->interface_setting_model->where(['option_name'=>'park_settings'])->find();

        $park = json_decode($options['option_value'],true);
        $options = array(
            'id'=>$options['option_id'],
            'user_name'=>$park['user_name'],
            'user_pwd'=>$park['user_pwd'],
            'user_key'=>$park['user_key'],
            );

        $this->assign('post',$options);
        $this->display();
    }

    public function park_setting_post()
    {
        if (IS_POST) {
            $post_id=intval($_POST['post']['id']);

            $interface=I("post.post");
            $data = array(
                'user_name'=>$interface['user_name'],
                'user_pwd'=>$interface['user_pwd'],
                'user_key'=>$interface['user_key'],
                );
            $interface['option_value']=json_encode($data);

            $result=$this->interface_setting_model->where(['id'=>$post_id])->save($interface);
            if ($result!==false) {
                $this->success("保存成功！",U("AdminSettings/park"));
            } else {
                $this->error("保存失败！");
            }
        }
    }
    /**
     * 停车系统接口列表
     * @return [type] [description]
     */
    public function park()
    {
        $where['inter_status']=array(array('neq','-1'));
        $where['inter_type']=array(array('eq','park'));

        $this->interface_model
        ->alias("a")
        ->where($where);

        $count=$this->interface_model->count();

        $page = $this->page($count, 20);


        $this->interface_model
        ->alias("a")
        ->where($where)
        ->limit($page->firstRow , $page->listRows)
        ->order("a.create_time DESC");
        $posts=$this->interface_model->select();


        $this->assign("page", $page->show('Admin'));
        $this->assign("formget",array_merge($_GET,$_POST));
        $this->assign("posts",$posts);

        $this->display();
    }

    /**
     * ERP系统接口列表
     * @return [type] [description]
     */
    public function erp()
    {
        $where['inter_status']=array(array('neq','-1'));
        $where['inter_type']=array(array('eq','erp'));

        $this->interface_model
        ->alias("a")
        ->where($where);

        $count=$this->interface_model->count();

        $page = $this->page($count, 20);


        $this->interface_model
        ->alias("a")
        ->where($where)
        ->limit($page->firstRow , $page->listRows)
        ->order("a.create_time DESC");
        $posts=$this->interface_model->select();


        $this->assign("page", $page->show('Admin'));
        $this->assign("formget",array_merge($_GET,$_POST));
        $this->assign("posts",$posts);

        $this->display();
    }

    public function add(){
        $this->display();
    }

    public function add_post(){
        if (IS_POST) {
            $interface=I("post.post");
            $interface['inter_status'] = '0';
            $interface['create_time'] = date("Y-m-d H:i:s",time());
            $interface['update_time'] = date("Y-m-d H:i:s",time());
            $result=$this->interface_model->add($interface);

            if ($result) {
                if($interface['inter_type'] == 'park'){
                    $this->success("添加成功！",U('AdminSettings/park'));
                }else{
                    $this->success("添加成功！",U('AdminSettings/erp'));
                }

            } else {
                $this->error("添加失败！");
            }
        }
    }

    public function edit(){
        $id = I("get.id",0,'intval');
        $data=$this->interface_model->where(array("id" => $id))->find();

        $this->assign("data",$data);
        $this->display();
    }

    public function edit_post(){
        if (IS_POST) {
            $post_id=intval($_POST['post']['id']);

            $interface=I("post.post");

            $interface['update_time'] = date("Y-m-d H:i:s",time());

            $result=$this->interface_model->where(['id'=>$post_id])->save($interface);

            if ($result) {
                $this->success("编辑成功！",U('AdminSettings/park'));
            } else {
                $this->error("添加失败！");
            }
        }
    }

    public function delete(){
        if(isset($_GET['id'])){
            $id = I("get.id",0,'intval');
            if ($this->interface_model->where(array('id'=>$id))->save(array('inter_status'=>"-1")) !==false) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }
    }

    public function version(){
        $host = $_SERVER['HTTP_HOST'];
        $dir = SITE_PATH."version/apk/";
        $dir2 = SITE_PATH."version/ios/";
        // $url = $host."/version/";
        $url = "";
        $res = $this->dir_size($dir,$url);
        $res2 = $this->dir_size($dir2,$url);

        $this->assign("res",$res);
        $this->assign("resios",$res2);

        $this->display();
    }

    private function dir_size($dir,$url){
     $dh = @opendir($dir);             //打开目录，返回一个目录流
     $return = array();
      $i = 0;
      while($file = @readdir($dh)){     //循环读取目录下的文件
        if($file!='.' and $file!='..'){
          $path = $dir.'/'.$file;     //设置目录，用于含有子目录的情况
          if(is_dir($path)){
          }elseif(is_file($path)){
              $filesize2[] =  round((filesize($path)/1024),2);//获取文件大小
              $filename2[] = $path;//获取文件名称
              $filetime2[] = date("Y-m-d H:i:s",filemtime($path));//获取文件最近修改日期

              $filesize =  round((filesize($path)/1024),2);//获取文件大小
              $filename = $path;//获取文件名称
              $filetime = date("Y-m-d H:i:s",filemtime($path));//获取文件最近修改日期
              $url = $url.'/'.$file;
              // $return[] =  $url.'/'.$file;
              $return[] =  ['name'=>$file,'size'=>$filesize,'url'=>$url,'time'=>$filetime];

          }
        }
      }
      @closedir($dh);             //关闭目录流
      // array_multisort($filesize,SORT_DESC,SORT_NUMERIC, $return);//按大小排序
      // array_multisort($filename,SORT_DESC,SORT_STRING, $files);//按名字排序
      array_multisort($filetime2,SORT_DESC,SORT_STRING, $return);//按时间排序
      return $return;               //返回文件
    }

}
